$(function () {
    var type = getUrlParam("type");
    switch (type) {
        case 'admin':
            $('#view-admin').show();
            var nav = getUrlParam("nav");
            switch (nav) {
                case "1":
                    $('#admin-nav-attractions').addClass('active');
                    $('#view-admin-attractions').show();
                    initAdminAttractionsTable(1);
                    break;
                case "2":
                    $('#admin-nav-orders').addClass('active');
                    $('#view-admin-orders').show();
                    initAdminOrdersTable(1);
                    break;
                case "3":
                    $('#admin-nav-users').addClass('active');
                    $('#view-admin-users').show();
                    initAdminUsersTable(1);
                    break;
                default:
                    $('#admin-nav-attractions').addClass('active');
                    $('#view-admin-attractions').show();
                    initAdminAttractionsTable(1);
                    break;
            }
            break;
        case 'user':
            $('#view-user').show();
            $('#user-nav-attractions').click(function () {
               window.location = "?type=user&phone=" + getUrlParam("phone") + "&nav=1";
            });
            $('#user-nav-orders').click(function () {
                window.location = "?type=user&phone=" + getUrlParam("phone") + "&nav=2";
            });
            $('#user-nav-info').click(function () {
                window.location = "?type=user&phone=" + getUrlParam("phone") + "&nav=3";
            });
            var nav_user = getUrlParam("nav");
            switch (nav_user) {
                case "1":
                    $('#user-nav-attractions').addClass('active');
                    $('#view-user-attractions').show();
                    initUserAttractionsTable(1);
                    break;
                case "2":
                    $('#user-nav-orders').addClass('active');
                    $('#view-user-orders').show();
                    initUserOrdersTable(1);
                    break;
                case "3":
                    $('#user-nav-info').addClass('active');
                    $('#view-user-info').show();
                    initUserInfo();
                    break;
                default:
                    $('#user-nav-attractions').addClass('active');
                    $('#view-user-attractions').show();
                    initUserAttractionsTable(1);
                    break;
            }
            break;
        default:
            $('#view-login').show();
            break;
    }
});

/**
 * 初始化用户信息
 */
function initUserInfo() {
    $.ajax({
        type: 'post',
        url: '/index',
        dataType: "json",
        data: {
            action: 'ACTION_GET_USER_INFO',
            user_id: getUrlParam("phone")
        },
        success: function (res) {
            if (res != null) {
                $('#view-user-info-name').val(res.name);
                $('#view-user-info-sex').val(res.sex);
                $('#view-user-info-age').val(res.age);
                $('#view-user-info-address').val(res.address);
            } else {
                console.log('无数据');
            }
        },
        error: function () {
            console.log('无数据');
        }
    });
}

/**
 * 保存用户信息
 */
function saveUserInfo() {
    $.ajax({
        type: 'post',
        url: '/index',
        dataType: "json",
        data: {
            action: 'ACTION_SET_USER_INFO',
            user_id: getUrlParam("phone"),
            name: $('#view-user-info-name').val(),
            sex: $('#view-user-info-sex').val(),
            age: $('#view-user-info-age').val(),
            address: $('#view-user-info-address').val()
        },
        success: function (res) {
            if (res != null) {
                alert('保存成功！');
            } else {
                alert('保存失败！');
            }
        },
        error: function () {
            alert('服务器异常！');
        }
    });
}

/**
 * 初始化订单信息表
 * @param pageNumber
 */
function initUserOrdersTable(pageNumber) {
    //初始化表格
    var table = new TableInit();
    //配置表格参数
    var parm = {
        url:'/index',
        id:'#view-user-orders-table',
        toolbar: '',
        pageNumber:pageNumber,
        data:{
            action: 'ACTION_GET_USER_ORDER',
            user_id: getUrlParam("phone")
        },
        search:true,
        export:false,
        columns:[{
            field: 'name',
            title: '景点名称',
            align: 'center'
        },{
            field: 'sort',
            title: '门票类型',
            align: 'center',
            formatter: function (value, row, index) {
                switch (value) {
                    case 0:
                        return '成人票';
                    case 1:
                        return '儿童票';
                    case 2:
                        return '老年票';
                    default:
                        return '成人票';
                }
            }
        },{
            field: 'price',
            title: '门票价格',
            align: 'center'
        },{
            field: 'time',
            title: '订购日期',
            align: 'center'
        },{
            field: 'id',
            title: '操作',
            align: 'center',
            formatter: function (value, row, index) {
                var data = escape(JSON.stringify(row));
                return "<a href='#' onclick='delUserOrders(\"" + data + "\")'>退票</a>";
            }
        }]
    };
    //创建表格
    table.Init(parm);
}

/**
 * 退票
 * @param data
 */
function delUserOrders(data) {
    data = JSON.parse(unescape(data));
    if (confirm('确定要退票吗？')) {
        $.ajax({
            type: 'post',
            url: '/index',
            dataType: "json",
            data: {
                action: 'ACTION_DEL_ORDER',
                id: data.id,
                ticket_id: data.ticket_id
            },
            success: function (res) {
                if (res) {
                    alert('退票成功！');
                    initUserOrdersTable(1);
                } else {
                    alert('退票失败！');
                }
            },
            error: function () {
                alert('服务器异常！');
            }
        });
    }
}

/**
 * 初始化订单信息表
 * @param pageNumber
 */
function initAdminOrdersTable(pageNumber) {
    //初始化表格
    var table = new TableInit();
    //配置表格参数
    var parm = {
        url:'/index',
        id:'#view-admin-orders-table',
        toolbar: '',
        pageNumber:pageNumber,
        data:{
            action: 'ACTION_GET_ADMIN_ORDER'
        },
        search:true,
        export:false,
        columns:[{
            field: 'user_id',
            title: '用户',
            align: 'center'
        },{
            field: 'name',
            title: '景点名称',
            align: 'center'
        },{
            field: 'sort',
            title: '门票类型',
            align: 'center',
            formatter: function (value, row, index) {
                switch (value) {
                    case 0:
                        return '成人票';
                    case 1:
                        return '儿童票';
                    case 2:
                        return '老年票';
                    default:
                        return '成人票';
                }
            }
        },{
            field: 'price',
            title: '门票价格',
            align: 'center'
        },{
            field: 'time',
            title: '订购日期',
            align: 'center'
        },{
            field: 'id',
            title: '操作',
            align: 'center',
            formatter: function (value, row, index) {
                var data = escape(JSON.stringify(row));
                return "<a href='#' onclick='delAdminOrders(\"" + data + "\")'>退票</a>";
            }
        }]
    };
    //创建表格
    table.Init(parm);
}

/**
 * 退票
 * @param data
 */
function delAdminOrders(data) {
    data = JSON.parse(unescape(data));
    if (confirm('确定要退票吗？')) {
        $.ajax({
            type: 'post',
            url: '/index',
            dataType: "json",
            data: {
                action: 'ACTION_DEL_ORDER',
                id: data.id,
                ticket_id: data.ticket_id
            },
            success: function (res) {
                if (res) {
                    alert('退票成功！');
                    initAdminOrdersTable(1);
                } else {
                    alert('退票失败！');
                }
            },
            error: function () {
                alert('服务器异常！');
            }
        });
    }
}

/**
 * 初始化景点信息表
 * @param pageNumber
 */
function initUserAttractionsTable(pageNumber) {
    //初始化表格
    var table = new TableInit();
    //配置表格参数
    var parm = {
        url:'/index',
        id:'#view-user-attractions-table',
        toolbar: '',
        pageNumber:pageNumber,
        data:{
            action: 'ACTION_GET_ATTRACTIONS'
        },
        search:true,
        export:false,
        columns:[{
            field: 'name',
            title: '景点名称',
            align: 'center'
        },{
            field: 'description',
            title: '景点介绍',
            align: 'center'
        },{
            field: 'address',
            title: '景点地址',
            align: 'center'
        },{
            field: 'level',
            title: '景点星级',
            align: 'center'
        },{
            field: 'id',
            title: '门票信息',
            align: 'center',
            formatter: function (value, row, index) {
                return "<a href='#' data-toggle=\"modal\" data-target=\"#user-dialog-tickets\" onclick='initUserTicketTable(1, \"" + value + "\")'>查看</a>";
            }
        }]
    };
    //创建表格
    table.Init(parm);
}

/**
 * 查看门票信息
 * @param pageNumber
 * @param id
 */
function initUserTicketTable(pageNumber, id) {
    attractions_id = id;
    //初始化表格
    var table = new TableInit();
    //配置表格参数
    var parm = {
        url:'/index',
        id:'#user-dialog-tickets-table',
        toolbar: '',
        pageNumber:pageNumber,
        data:{
            action: 'ACTION_GET_TICKETS',
            attractions_id: id
        },
        search:true,
        export:false,
        columns:[{
            field: 'sort',
            title: '门票类型',
            align: 'center',
            formatter: function (value, row, index) {
                switch (value) {
                    case 0:
                        return '成人票';
                    case 1:
                        return '儿童票';
                    case 2:
                        return '老年票';
                    default:
                        return '成人票';
                }
            }
        },{
            field: 'price',
            title: '门票价格',
            align: 'center'
        },{
            field: 'count',
            title: '门票余量',
            align: 'center'
        },{
            field: 'id',
            title: '操作',
            align: 'center',
            formatter: function (value, row, index) {
                return "<div class='btn-group-vertical'><button class='btn btn-info' onclick='userAddOrder(\"" + value + "\")'><span class='glyphicon glyphicon-edit'></span>&nbsp;订购</button></div>";
            }
        }]
    };
    //创建表格
    table.Init(parm);
}

/**
 * 订购门票
 * @param id
 */
function userAddOrder(id) {
    if (confirm('确定要订购该门票吗？')) {
        $.ajax({
            type: 'post',
            url: '/index',
            dataType: "json",
            data: {
                action: 'ACTION_ADD_ORDER',
                user_id: getUrlParam("phone"),
                ticket_id: id,
                time: getNowFormatDate()
            },
            success: function (res) {
                if (res) {
                    alert('订购成功！');
                    initUserTicketTable(1, attractions_id);
                } else {
                    alert('订购失败！');
                }
            },
            error: function () {
                alert('服务器异常！');
            }
        });
    }
}

/**
 * 初始化景点信息表
 * @param pageNumber
 */
function initAdminUsersTable(pageNumber) {
    //初始化表格
    var table = new TableInit();
    //配置表格参数
    var parm = {
        url:'/index',
        id:'#view-admin-users-table',
        toolbar: '',
        pageNumber:pageNumber,
        data:{
            action: 'ACTION_GET_USERS'
        },
        search:true,
        export:false,
        columns:[{
            field: 'id',
            title: '编号',
            align: 'center'
        },{
            field: 'phone',
            title: '账号',
            align: 'center'
        },{
            field: 'id',
            title: '操作',
            align: 'center',
            formatter: function (value, row, index) {
                //var data = escape(JSON.stringify(row));
                return "<a href='#' data-toggle=\"modal\"  data-target=\"#admin-dialog-s\" onclick='adminIntUserInfo(1, \"" + value + "\")'>用户信息</a>";
                //return "<div class='btn-group-vertical'><button data-toggle=\"modal\" data-target=\"#admin-dialog-users\" class='btn btn-info' onclick='adminIntUserInfo(\"" + value + "\")'><span class='glyphicon glyphicon-edit'></span>&nbsp;用户信息</button></div>";
            }
        }]
    };
    //创建表格
    table.Init(parm);
}

/**
 * 初始化用户信息表
 * @param pageNumber
 */
function adminIntUserInfo(pageNumber) {
    //初始化表格
    var table = new TableInit();
    //配置表格参数
    var parm = {
        url:'/index',
        id:'#view-user-users-table',
        toolbar: '#view-user-users-b',
        pageNumber:pageNumber,
        data:{
            action: 'ACTION_GET_ATTRACTIONS'
        },
        search:true,
        export:false,
        columns:[{
            field: 'name',
            title: '用户姓名',
            align: 'center'
        },{
            field: 'sex',
            title: '用户性别',
            align: 'center'
        },{
            field: 'age',
            title: '用户年龄',
            align: 'center'
        },{
            field: 'address',
            title: '用户住址',
            align: 'center'

        }]
    };
    //创建表格
    table.Init(parm);
}

/**
 * 修改用户密码
 * @param id
 */
function adminEditUserPwd(id) {
    $('#admin-dialog-users-id').val(id);
    $('#admin-dialog-users-pwd').val('');
}

/**
 * 修改用户密码
 */
function doAdminEditUserPwd() {
    var id = $('#admin-dialog-users-id').val();
    var pwd = $('#admin-dialog-users-pwd').val();
    if (pwd === '') {
        alert('请输入密码！');
        return;
    }
    $.ajax({
        type: 'post',
        url: '/index',
        dataType: "json",
        data: {
            action: 'ACTION_UPDATE_USER_PWD',
            id: id,
            pwd: pwd
        },
        success: function (res) {
            if (res) {
                alert('修改成功！');
                $('#admin-dialog-users').modal('hide');
            } else {
                alert('修改失败！');
            }
        },
        error: function () {
            alert('服务器异常！');
        }
    });
}

/**
 * 初始化景点信息表
 * @param pageNumber
 */
function initAdminAttractionsTable(pageNumber) {
    //初始化表格
    var table = new TableInit();
    //配置表格参数
    var parm = {
        url:'/index',
        id:'#view-admin-attractions-table',
        toolbar: '#view-admin-attractions-toolbar',
        pageNumber:pageNumber,
        data:{
            action: 'ACTION_GET_ATTRACTIONS'
        },
        search:true,
        export:false,
        columns:[{
            field: 'name',
            title: '景点名称',
            align: 'center'
        },{
            field: 'description',
            title: '景点介绍',
            align: 'center'
        },{
            field: 'address',
            title: '景点地址',
            align: 'center'
        },{
            field: 'level',
            title: '景点星级',
            align: 'center'
        },{
            field: 'id',
            title: '门票信息',
            align: 'center',
            formatter: function (value, row, index) {
                return "<a href='#' data-toggle=\"modal\" data-target=\"#admin-dialog-tickets\" onclick='initAdminTicketTable(1, \"" + value + "\")'>查看</a>";
            }
        },{
            field: 'id',
            title: '销售情况',
            align: 'center',
            formatter: function (value, row, index) {
                return "<a href='#' data-toggle=\"modal\" data-target=\"#admin-dialog-sales\" onclick='initAdminSalesTable(1, \"" + value + "\")'>查看</a>";
            }
        },{
            field: 'id',
            title: '操作',
            align: 'center',
            formatter: function (value, row, index) {
                var data = escape(JSON.stringify(row));
                return "<div class='btn-group-vertical'><button data-toggle=\"modal\" data-target=\"#admin-dialog-attractions\" class='btn btn-info' onclick='adminAttractions(\"" + data + "\")'><span class='glyphicon glyphicon-edit'></span>&nbsp;修改</button><button class='btn btn-danger' onclick='adminDelAttractions(\"" + value + "\")'><span class='glyphicon glyphicon-remove'></span>&nbsp;删除</button></div>";
            }
        }]
    };
    //创建表格
    table.Init(parm);
}

/**
 * 初始化销售情况信息表
 * @param id
 * @param pageNumber
 */
function initAdminSalesTable(id, pageNumber) {
    //初始化表格
    var table = new TableInit();
    //配置表格参数
    var parm = {
        url:'/index',
        id:'#admin-dialog-sales-table',
        toolbar: '',
        pageNumber:pageNumber,
        data:{
            action: 'ACTION_GET_SALES',
            attractions_id: id
        },
        search:true,
        export:false,
        columns:[{
            field: 'user_id',
            title: '用户',
            align: 'center'
        },{
            field: 'name',
            title: '景点名称',
            align: 'center'
        },{
            field: 'sort',
            title: '门票类型',
            align: 'center',
            formatter: function (value, row, index) {
                switch (value) {
                    case 0:
                        return '成人票';
                    case 1:
                        return '儿童票';
                    case 2:
                        return '老年票';
                    default:
                        return '成人票';
                }
            }
        },{
            field: 'price',
            title: '门票价格',
            align: 'center'
        },{
            field: 'time',
            title: '订购日期',
            align: 'center'
        }]
    };
    //创建表格
    table.Init(parm);
}

// 景点编号
var attractions_id = null;

/**
 * 查看门票信息
 * @param pageNumber
 * @param id
 */
function initAdminTicketTable(pageNumber, id) {
    attractions_id = id;
    //初始化表格
    var table = new TableInit();
    //配置表格参数
    var parm = {
        url:'/index',
        id:'#admin-dialog-tickets-table',
        toolbar: '#admin-dialog-tickets-toolbar',
        pageNumber:pageNumber,
        data:{
            action: 'ACTION_GET_TICKETS',
            attractions_id: id
        },
        search:true,
        export:false,
        columns:[{
            field: 'sort',
            title: '门票类型',
            align: 'center',
            formatter: function (value, row, index) {
                switch (value) {
                    case 0:
                        return '成人票';
                    case 1:
                        return '儿童票';
                    case 2:
                        return '老年票';
                    default:
                        return '成人票';
                }
            }
        },{
            field: 'price',
            title: '门票价格',
            align: 'center'
        },{
            field: 'count',
            title: '门票余量',
            align: 'center'
        },{
            field: 'id',
            title: '操作',
            align: 'center',
            formatter: function (value, row, index) {
                var data = escape(JSON.stringify(row));
                return "<div class='btn-group-vertical'><button data-toggle=\"modal\" data-target=\"#admin-dialog-tickets-info\" class='btn btn-info' onclick='adminEditTicket(\"" + data + "\")'><span class='glyphicon glyphicon-edit'></span>&nbsp;修改</button><button class='btn btn-danger' onclick='adminDelTicket(\"" + value + "\")'><span class='glyphicon glyphicon-remove'></span>&nbsp;删除</button></div>";
            }
        }]
    };
    //创建表格
    table.Init(parm);
}

/**
 * 添加门票
 */
function adminAddTicket() {
    $('#admin-dialog-tickets-info-label').text('新建门票');
    $('#admin-dialog-tickets-info-btn-edit').hide();
    $('#admin-dialog-tickets-info-btn-add').show();
    $('#admin-dialog-tickets-info-price').val('');
    $('#admin-dialog-tickets-info-count').val('');
}

/**
 * 添加门票
 */
function doAdminAddTicket() {
    var sort = $('#admin-dialog-tickets-info-sort').val();
    var price = $('#admin-dialog-tickets-info-price').val();
    var count = $('#admin-dialog-tickets-info-count').val();
    if (price === '') {
        alert('请输入门票价格！');
        return;
    }
    if (count === '') {
        alert('请输入门票余量！');
        return;
    }
    $.ajax({
        type: 'post',
        url: '/index',
        dataType: "json",
        data: {
            action: 'ACTION_ADD_TICKETS',
            attractions_id: attractions_id,
            sort: sort,
            price: price,
            count: count
        },
        success: function (res) {
            if (res) {
                alert('新建成功！');
                $('#admin-dialog-tickets-info').modal('hide');
                initAdminTicketTable(1, attractions_id);
            } else {
                alert('新建失败！');
            }
        },
        error: function () {
            alert('服务器异常！');
        }
    });
}

/**
 * 修改门票
 * @param data
 */
function adminEditTicket(data) {
    var data = JSON.parse(unescape(data));
    $('#admin-dialog-tickets-info-label').text('修改门票');
    $('#admin-dialog-tickets-info-btn-edit').show();
    $('#admin-dialog-tickets-info-btn-add').hide();
    $('#admin-dialog-tickets-info-id').val(data.id);
    $('#admin-dialog-tickets-info-sort').val(data.sort);
    $('#admin-dialog-tickets-info-price').val(data.price);
    $('#admin-dialog-tickets-info-count').val(data.count);
}

/**
 * 修改门票
 */
function doAdminEditTicket() {
    var id = $('#admin-dialog-tickets-info-id').val();
    var sort = $('#admin-dialog-tickets-info-sort').val();
    var price = $('#admin-dialog-tickets-info-price').val();
    var count = $('#admin-dialog-tickets-info-count').val();
    if (price === '') {
        alert('请输入门票价格！');
        return;
    }
    if (count === '') {
        alert('请输入门票余量！');
        return;
    }
    $.ajax({
        type: 'post',
        url: '/index',
        dataType: "json",
        data: {
            action: 'ACTION_UPDATE_TICKETS',
            id: id,
            sort: sort,
            price: price,
            count: count
        },
        success: function (res) {
            if (res) {
                alert('修改成功！');
                $('#admin-dialog-tickets-info').modal('hide');
                initAdminTicketTable($('#admin-dialog-tickets-table').bootstrapTable('getOptions').pageNumber, attractions_id);
            } else {
                alert('修改失败！');
            }
        },
        error: function () {
            alert('服务器异常！');
        }
    });
}

/**
 * 删除门票
 * @param id
 */
function adminDelTicket(id) {
    if (confirm('确定要删除该门票吗？')) {
        $.ajax({
            type: 'post',
            url: '/index',
            dataType: "json",
            data: {
                action: 'ACTION_DEL_TICKETS',
                id: id
            },
            success: function (res) {
                if (res) {
                    alert('删除成功！');
                    initAdminTicketTable($('#admin-dialog-tickets-table').bootstrapTable('getOptions').pageNumber, attractions_id);
                } else {
                    alert('删除失败！');
                }
            },
            error: function () {
                alert('服务器异常！');
            }
        });
    }
}

/**
 * 添加景点
 */
function adminAddAttractions() {
    $('#admin-dialog-attractions-label').text('新建景点');
    $('#admin-dialog-attractions-btn-edit').hide();
    $('#admin-dialog-attractions-btn-add').show();
    $('#admin-dialog-attractions-name').val('');
    $('#admin-dialog-attractions-description').val('');
    $('#admin-dialog-attractions-address').val('');
    $('#admin-dialog-attractions-level').val('');
}

/**
 * 添加景点
 */
function doAdminAddAttractions() {
    var name = $('#admin-dialog-attractions-name').val();
    var description = $('#admin-dialog-attractions-description').val();
    var address = $('#admin-dialog-attractions-address').val();
    var level = $('#admin-dialog-attractions-level').val();
    if (name === '') {
        alert('请输入景点名称！');
        return;
    }
    if (description === '') {
        alert('请输入景点描述！');
        return;
    }
    if (address === '') {
        alert('请输入景点地址！');
        return;
    }
    if (level === '') {
        alert('请输入景点星级！');
        return;
    }
    $.ajax({
        type: 'post',
        url: '/index',
        dataType: "json",
        data: {
            action: 'ACTION_ADD_ATTRACTIONS',
            name: name,
            description: description,
            address: address,
            level: level
        },
        success: function (res) {
            if (res) {
                alert('新建成功！');
                $('#admin-dialog-attractions').modal('hide');
                initAdminAttractionsTable(1);
            } else {
                alert('新建失败！');
            }
        },
        error: function () {
            alert('服务器异常！');
        }
    });
}

/**
 * 修改景点
 * @param data
 */
function adminAttractions(data) {
    var data = JSON.parse(unescape(data));
    $('#admin-dialog-attractions-label').text('修改景点');
    $('#admin-dialog-attractions-btn-edit').show();
    $('#admin-dialog-attractions-btn-add').hide();
    $('#admin-dialog-attractions-id').val(data.id);
    $('#admin-dialog-attractions-name').val(data.name);
    $('#admin-dialog-attractions-description').val(data.description);
    $('#admin-dialog-attractions-address').val(data.address);
    $('#admin-dialog-attractions-level').val(data.level);
}

/**
 * 修改景点
 */
function doAdminEditAttractions() {
    var id = $('#admin-dialog-attractions-id').val();
    var name = $('#admin-dialog-attractions-name').val();
    var description = $('#admin-dialog-attractions-description').val();
    var address = $('#admin-dialog-attractions-address').val();
    var level = $('#admin-dialog-attractions-level').val();
    if (name === '') {
        alert('请输入景点名称！');
        return;
    }
    if (description === '') {
        alert('请输入景点描述！');
        return;
    }
    if (address === '') {
        alert('请输入景点地址！');
        return;
    }
    if (level === '') {
        alert('请输入景点星级！');
        return;
    }
    $.ajax({
        type: 'post',
        url: '/index',
        dataType: "json",
        data: {
            action: 'ACTION_UPDATE_ATTRACTIONS',
            name: name,
            description: description,
            address: address,
            level: level,
            id: id
        },
        success: function (res) {
            if (res) {
                alert('修改成功！');
                $('#admin-dialog-attractions').modal('hide');
                initAdminAttractionsTable($('#view-admin-attractions-table').bootstrapTable('getOptions').pageNumber);
            } else {
                alert('修改失败！');
            }
        },
        error: function () {
            alert('服务器异常！');
        }
    });
}

/**
 * 删除景点
 * @param id
 */
function adminDelAttractions(id) {
    if (confirm('确定要删除该景点吗？')) {
        $.ajax({
            type: 'post',
            url: '/index',
            dataType: "json",
            data: {
                action: 'ACTION_DEL_ATTRACTIONS',
                id: id
            },
            success: function (res) {
                if (res) {
                    alert('删除成功！');
                    initAdminAttractionsTable($('#view-admin-attractions-table').bootstrapTable('getOptions').pageNumber);
                } else {
                    alert('删除失败！');
                }
            },
            error: function () {
                alert('服务器异常！');
            }
        });
    }
}

/**
 * 立即注册
 */
$('#to_sign').click(function () {
    $('#login').hide();
    $('#to_sign').hide();
    $('#sign').show();
    $('#to_login').show();
});

/**
 * 返回登录
 */
$('#to_login').click(function () {
    $('#sign').hide();
    $('#to_login').hide();
    $('#login').show();
    $('#to_sign').show();
});

/**
 * 登录
 */
$('#login').click(function () {
    var phone = $('#phone').val();
    var pwd = $('#pwd').val();

    if (phone === '') {
        alert('请输入账户！');
        return;
    }
    if (pwd === '') {
        alert('请输入密码！');
        return;
    }

    var data = {
        action: 'ACTION_LOGIN',
        phone: phone,
        pwd: pwd
    };

    $.ajax({
        type: 'post',
        url: '/index',
        dataType: "json",
        data: data,
        success: function (res) {
            if (res) {
                if (phone === 'admin') {
                    window.location = "?type=admin";
                } else {
                    window.location = "?type=user&phone=" + phone;
                }
            } else {
                alert('账号/密码错误！');
            }
        },
        error: function () {
            alert('账号/密码错误！');
        }
    });
});

/**
 * 注册
 */
$('#sign').click(function () {
    var phone = $('#phone').val();
    var pwd = $('#pwd').val();

    if (phone === '') {
        alert('请输入账户！');
        return;
    }
    if (pwd === '') {
        alert('请输入密码！');
        return;
    }

    var data = {
        action: 'ACTION_SIGN',
        phone: phone,
        pwd: pwd
    };

    $.ajax({
        type: 'post',
        url: '/index',
        dataType: "json",
        data: data,
        success: function (res) {
            if (res) {
                window.location = "?type=user&phone=" + phone;
            } else {
                alert('该账号已被注册！');
            }
        },
        error: function () {
            alert('该账号已被注册！');
        }
    });
});

/**
 * 获取url中的指定参数
 * @param {string} name
 */
function getUrlParam(name) {
    //构造一个含有目标参数的正则表达式对象
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    //匹配目标参数
    var r = window.location.search.substr(1).match(reg);
    //返回参数值
    if (r != null)
        return decodeURI(r[2]);
    return null;
}

/**
 * 获取当前日期
 * @returns {string}
 */
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}